import 'dart:convert';

const DownloadStatusPending = "等待";
const DownloadStatusParsing = "解析中";
const DownloadStatusDownloading = "下载中";
const DownloadStatusPaused = "暂停";
const DownloadStatusMerging = "合成中";
const DownloadStatusCompleted = "下载完成";
const DownloadStatusCancel = "已取消";
const DownloadStatusFailed = "失败";

class Download {
  int? id;
  String? site;
  String? url;
  String? name;
  String? savePath;
  int? progress;
  List<String>? segments;
  int? downloadIndex;
  String? status;
  String? createdAt;
  String? movie;

  Download({
    this.id,
    this.site,
    this.url,
    this.name,
    this.savePath,
    this.progress,
    this.segments,
    this.downloadIndex,
    this.status,
    this.movie,
    this.createdAt,
  });

  Download.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    site = json['site']?.toString();
    url = json['url']?.toString();
    name = json['name']?.toString();
    savePath = json['save_path']?.toString();
    progress = json['progress'];
    if (json['segments'] != null) {
      segments = jsonDecode(json['segments'])
          .map<String>((e) => e.toString())
          .toList();
    }
    downloadIndex = json['download_index'];
    status = json['status']?.toString();
    movie = json['movie']?.toString();
    createdAt = json['created_at']?.toString();
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'site': site,
      'url': url,
      'name': name,
      'save_path': savePath,
      'progress': progress,
      'segments': jsonEncode(segments),
      'download_index': downloadIndex,
      'status': status,
      'movie': movie,
      'created_at': createdAt,
    };
  }
}
